﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using backfile.util;

namespace backfile
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 源目录选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void source_button_Click(object sender, EventArgs e)
        {
            string path = selectPath();

            SortPathFroms();

            if (path != "")
            {
                if (this.textBox3.Text != "")
                {
                    this.textBox3.Text += Environment.NewLine;//换行
                }
                this.textBox3.Text += path;
            }
        }

        /// <summary>
        /// 目标目录选择
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void target_button_Click(object sender, EventArgs e)
        {
            string path = selectPath();

            SortPathTo();

            if (path != "")
            {
                if (this.textBox4.Text != "")
                {
                    this.textBox4.Text += Environment.NewLine;//换行
                }
                this.textBox4.Text += path;
            }
        }

        /// <summary>
        /// 目标目录文本框显示
        /// </summary>
        private void SortPathTo()
        {
            string[] paths = getPathTo();

            if (paths.Length > 0)
            {
                this.textBox4.Text = string.Join(Environment.NewLine, paths);

            }
            else
            {
                this.textBox4.Text = "";
            }
        }
        /// <summary>
        /// 源目录文本框显示
        /// </summary>
        private void SortPathFroms()
        {
            string[] paths = getPathForms();

            if (paths.Length > 0)
            {
                this.textBox3.Text = string.Join(Environment.NewLine, paths);

            }
            else
            {
                this.textBox3.Text = "";
            }
        }
        /// <summary>
        /// 获取源目录信息
        /// </summary>
        /// <returns></returns>
        private string[] getPathForms()
        {
            string[] pathForms = this.textBox3.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < pathForms.Length; i++)
            {
                pathForms[i] = pathForms[i].Trim();//去掉首尾空格
            }
            return pathForms.Distinct().ToArray();
        }

        /// <summary>
        /// 获取目标目录信息
        /// </summary>
        /// <returns></returns>
        private string[] getPathTo()
        {
            //return textFolderTo.Text;
            string[] pathTos = this.textBox4.Text.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < pathTos.Length; i++)
            {
                pathTos[i] = pathTos[i].Trim();//去掉首尾空格
            }
            return pathTos.Distinct().ToArray();
        }

        /// <summary>
        /// 显示选择目录框
        /// </summary>
        /// <returns></returns>
        private string selectPath()
        {
            string foldPath = "";
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "请选择文件路径";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                foldPath = dialog.SelectedPath;
            }
            return foldPath;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void submit_Click(object sender, EventArgs e)
        {
            string title = this.textBox1.Text.Trim();
            string start_date = this.dateTimePicker1.Value.ToString("yyy-MM-dd HH:mm:ss");
            string interval = this.textBox2.Text.Trim();
            string source_dir = this.textBox3.Text.Trim();
            string target_dir = this.textBox4.Text.Trim();
            string ignore_file = this.textBox5.Text.Trim();

            string is_compress = "否";
            string is_delete = "否";
            if (!this.radio_compress1.Checked && !this.radio_compress2.Checked)
            {
                MessageBox.Show("请选择是否压缩文件");
                return;
            }

            if (this.radio_compress1.Checked)
            {
                is_compress = "是";
            }

            if (!this.radiodelete1.Checked && !this.radiodelete2.Checked)
            {
                MessageBox.Show("请选择是否删除文件");
                return;
            }

            if (this.radiodelete1.Checked)
            {
                is_delete = "是";
            }
          
            string sql = "INSERT INTO task (title, start_date,source_dir,target_dir,interval,is_compress,is_delete,ignore_file,next_date,state )  VALUES ('" + title + "','" + start_date + "','"+ source_dir + "','" + target_dir + "','" + interval + "','" + is_compress + "','" + is_delete + "','" + ignore_file + "','" + start_date + "',0)";
            //MessageBox.Show(sql);
          
            int rs = SQLiteHelper.ExecuteNonQuery(SQLiteHelper.LocalDbConnectionString, sql,CommandType.Text);
            if (rs > 0)
            {
                DialogResult = DialogResult.OK;
                //MessageBox.Show("添加成功");
                MainForm.instance.ReadMainLog("添加任务标题："+ title+ "添加成功");
                this.Close();
            }
            else
            {
                MessageBox.Show("添加失败");
            }
         
        }
    }

 
}
